<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/7/2
 * Time: 2:45 PM
 */
namespace app\backend\modules\content\services;

use app\common\models\Article;

class ArticleService
{
    public function getList($params){
        $query = Article::site()->select();
        if($params['cid']){
            $query->where('cid',$params['cid']);
        }
        if($params['title']){
            $query->where('title','like','%'.$params['title'].'%');
        }
        $start_time = $params['start_time'];
        $end_time = $params['end_time'];
        if($start_time && $end_time){
            $query = $query->where('created_at','>=',strtotime($start_time));
            $query = $query->where('created_at','<=',strtotime($end_time));
        }else if($start_time){
            $query = $query->where('created_at','>=',strtotime($start_time));
        }else if($end_time){
            $query = $query->where('created_at','<=',strtotime($end_time));
        }
        $list = $query
            ->with(['category' => function($query) {
                return $query->select('id','name');
            }])
            ->orderBy('id', 'desc')
            ->paginate(10);
        $list->map(function ($v){
            $v->cname = '';
            if($v['category']&&$v['category']['name']){
                $v->cname = $v['category']['name'];
            }
            $v->publish_time = date('Y-m-d H:i:s',$v->publish_time);
        });
        return $list;
    }
}